package p.ithorns.tools.translator.sdk.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * MD5Codec
 *
 * @author ithorns
 * @version 1.0
 * @date 2019/5/27 14:41
 */
public class MD5Codec {

    private final static String CHARSET = "UTF-8";

    public static String md5(byte[] bytes) throws NoSuchAlgorithmException {
        // 确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        Base64.Encoder base64Encoder = Base64.getEncoder();
        // 加密字符串
        return base64Encoder.encodeToString(md5.digest(bytes));
    }

    public static String md5(String src) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if (src == null || src.isEmpty()) {
            return null;
        }
        return md5(src.getBytes(CHARSET));
    }

}